import java.util.Arrays;
import java.util.Scanner;

public class NGuessNumber {
    public static int[] numbers = new int[]{-1,-1,-1,-1};
    public static int[] guesslist = new int[]{-1,-1,-1,-1};
    public static int A_num = 0;
    public static int B_num = 0;

    public static void main(String[] args) {
        System.out.println("猜数字游戏你玩吗");

        while (true){
            System.out.println("输入1开始游戏,输入0结束游戏");
            System.out.println();
            Scanner playerinput = new Scanner(System.in);

            switch (playerinput.nextInt()){
                case 0:
                    System.out.println("退出游戏\n");
                    System.exit(0);
                    break;
                case 1:
                    int cs = 0;
                    int chances = 7;

                    System.out.println("已开始游戏(七次机会)\n");
                    generateAnswer();
                    while (true){

                        getPlayerInput();
                        compareGuessAnswer();
                        if (isWin() == true){
                            System.out.println("玩家获胜\n");
                            break;
                        }else {
                            cs++;
                            chances--;
                            if (cs == 7){
                                System.out.println("没机会了,正确答案是"+ Arrays.toString(numbers));
                                System.out.println("再来一局吗?");
                                break;
                            }
                            System.out.println("猜错了,重新猜,还有"+chances+"次机会");
                        }
                    }
                    break;

                default:
                    System.out.println("非法字符,请输入0/1\n");
            }
        }

    }

    //generateAnswer方法

    public static int[] generateAnswer(){
        //随机生成四个0-9之间的不重复的整数作为答案并返回的功能
        int index = 0;

        while(true){
            int random = (int)(Math.random() * 9+1 );
            int j;
            for (j=0;j< numbers.length;j++){
                if (random == numbers[j]){
                    break;
                }
            }
            if (j == numbers.length){
                numbers[index++] = random;
            }
            if (index == 4){
                break;
            }
        }
        return numbers;
    }

    //getPlayerInput方法

    public static int[] getPlayerInput(){
        //接受玩家输入的四个数字并返回
        try {
            Scanner scanner = new Scanner(System.in);

            for (int i = 0; i < 4; i++) {
                System.out.println("输入0-9之间的数字%%%%第"+(i+1)+"个数字");

                int answer1 = scanner.nextInt();
                if (answer1 > 9 || answer1 < 0){
                    System.out.println("你输入的数值不在0-9之间");
                    i=i + 1;
                }else {
                    guesslist[i] = answer1;
                }
            }
        }catch (Exception e){
            System.out.println("非法字符");
        }

        System.out.println(Arrays.toString(guesslist)+"\n");
        return guesslist;
    }

    //compareGuessAnswer方法
    public static String compareGuessAnswer(){
        A_num = 0;
        B_num = 0;
        //比较玩家猜测数字和答案并返回提示信息# A表示正确B表示错误 数字表示数量
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers.length; j++) {
                if (i!=j){
                    if (numbers[i]==guesslist[i]){
                        B_num++;
                    }
                }else {
                    if (numbers[i] == guesslist[j]){
                        A_num++;
                    }
                }
            }
        }
        System.out.println(A_num+"A"+B_num+"B");
        System.out.println("A表示存在且位置正确的数字个数,B表示存在且位置不正确的数字个数!");
        System.out.println("如: 1A2B,表示你猜测的四个数字有3个是对的,有一个位置对,两个位置不对.\n");

        return A_num+"A"+B_num+"B\n"+"A表示存在且位置正确的数字个数,B表示存在且位置不正确的数字个数! \n"+"如: 1A2B,表示你猜测的四个数字有3个是对的,有一个位置对,两个位置不对.\n";
    }

    //win方法
    public static boolean isWin(){
        boolean flag = false;
        //根据提示信息中A的数量判断玩家是否获胜,若玩家获胜返回true,否则返回false;
        if (A_num == 4){
            flag = true;
        }else {
            flag = false;
        }
        return flag;
    }
}



















